//
//  ViewController3.m
//  201-数据存储
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController3.h"

@interface ViewController3 ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameTextFiled;
@property (weak, nonatomic) IBOutlet UITextField *ageTextField;
@property (weak, nonatomic) IBOutlet UITextField *IDTextFiled;
@property (nonatomic,strong) NSMutableArray *stuArr;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation ViewController3

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
}

-(NSMutableArray *)stuArr{
    if (_stuArr == nil) {
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:[self plistPath]]) {
            _stuArr = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else{
            _stuArr = [NSMutableArray array];
        }
    }
    
    return _stuArr;
}

-(NSString *)plistPath{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"stu.plist"];
    return filePath;
}

-(void)save{
    [self.stuArr writeToFile:[self plistPath] atomically:YES];
    [self.tableView reloadData];
    
    
}
-(IBAction)add {
    NSMutableDictionary *dic = [@{@"stuID":self.IDTextFiled.text,
                          @"stuName":self.nameTextFiled.text,
                          @"stuAge":self.ageTextField.text} mutableCopy];
    [self.stuArr addObject:dic];
    [self save];
    
}
- (IBAction)delete {
    for (NSMutableDictionary *dic in self.stuArr) {
        NSLog(@"%@",dic[@"stuID"]);
        if ([dic[@"stuID"] isEqualToString:self.IDTextFiled.text]) {
            [self.stuArr removeObject:dic];
            //NSLog(@"%i",self.stuArr.count);
            break;
        }
    }
    [self save];
}//Users/student/Desktop/UI自己写的代码/201/201-数据存储/201-数据存储/ViewController3.m:74:13: Expected method to write dictionary element not found on object of type 'NSDictionary *'
- (IBAction)modify
{
    for (NSMutableDictionary *dic in self.stuArr)
    {
        NSLog(@"%@",dic);
        if ([dic[@"stuID"] isEqualToString:self.IDTextFiled.text])
        {
            dic[@"stuName"] = self.nameTextFiled.text;
            dic[@"stuAge"] = self.ageTextField.text;
        }
    }
    [self save];
}
- (IBAction)find {
    for (NSMutableDictionary *dic in self.stuArr) {
        if ([dic[@"stuID"] isEqualToString:self.IDTextFiled.text]) {
            self.nameTextFiled.text = dic[@"stuName"];
            self.ageTextField.text = dic[@"stuAge"];
        }
    }
}
- (IBAction)findAll:(id)sender {
    
    
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.stuArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    UILabel *labe1 = [cell.contentView viewWithTag:201];
    UILabel *labe2 = [cell.contentView viewWithTag:202];
    UILabel *labe3 = [cell.contentView viewWithTag:203];
    NSDictionary *m = self.stuArr[indexPath.row];
    labe3.text = m[@"stuID"];
    labe2.text = m[@"stuName"];
    labe1.text = m[@"stuAge"];
    
    return cell;

    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
